import java.util.concurrent.PriorityBlockingQueue;

class Task implements  Comparable<Task>{
    public Runnable runnable;  //任务；
    public  long time;       //时间
     public Runnable getRunnable(){
         return runnable;
     }
     public long getTime(){
         return time;
     }
     public void run(){
         runnable.run();
     }

    public Task(Runnable runnable, long time) {
        this.runnable = runnable;
        this.time = time;
    }

    @Override
    public int compareTo(Task o) {
        return (int)(this.time-o.time);
    }
}
public class MyTimer {

    private Thread thread=null;
    private PriorityBlockingQueue<Task> queue=new PriorityBlockingQueue<>();
    public MyTimer(){
              thread=new Thread(()->{

                 while(true) {
                     try {
                         synchronized (this)  {
                             Task task = queue.take();
                             long time1 = System.currentTimeMillis();
                             if (task.getTime() <time1) {
                                 queue.put(task);
                                 this.wait(task.getTime() - time1);

                             } else {
                                 task.run();
                             }
                         }
                     } catch (InterruptedException e) {
                         throw new RuntimeException(e);
                     }
                 }
        });
        thread.start();
    }

    public void schedule(Runnable runnable,long time){
        Task task=new Task(runnable,time+System.currentTimeMillis());
        queue.put(task);
        synchronized (this) {
            this.notify();
        }
    }

}
